﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using OpenCvSharp;
using OpenCvSharp.Blob;

namespace RecognitionNumbers
{
    public class MatrixSymbol
    {
        public List<double> matrix { get; set; }
        public int count { get; set; }
        public char symbol { get; set; }

        public void clear()
        {
            count = 0;
            matrix = new List<double>();
            for (int i = 1; i <= 20; i++)
                for (int j = 1; j <= 10; j++)
                    matrix.Add(0);
        }

        public void add(IplImage img)
        {
            CvSize size = new CvSize(10, 20);
            IplImage t = new IplImage(size, img.Depth, img.NChannels);
            Cv.Resize(img, t, Interpolation.Linear);
            Gray gray = new Gray(t);
            Binarization blackWhte = new Binarization(gray.output, 125);
            for (int i = 0; i < blackWhte.output.Height; i++)
                for (int j = 0; j < blackWhte.output.Width; j++)
                {
                    if (blackWhte.output[i, j].Val0 == 0)
                    {
                        matrix[(i) * blackWhte.output.Width + j] = (matrix[(i) * blackWhte.output.Width + j] * count + 1) / Convert.ToDouble(count + 1);
                    }
                    else
                    {
                        matrix[(i) * blackWhte.output.Width + j] = (matrix[(i) * blackWhte.output.Width + j] * count) / Convert.ToDouble(count + 1);
                    }
                }
            count++;
        }

        public double compare(IplImage img)
        {
            CvSize size = new CvSize(10, 20);
            IplImage t = new IplImage(size, img.Depth, img.NChannels);
            Cv.Resize(img, t, Interpolation.Linear);
            Gray gray = new Gray(t);
            Binarization blackWhite = new Binarization(gray.output, 125);
     
            double sum = 0;
            for (int i = 0; i < blackWhite.output.Height; i++)
                for (int j = 0; j < blackWhite.output.Width; j++)
                {
                    if (blackWhite.output[i, j].Val0 == 0)
                    {
                        sum += 1.0 - matrix[(i) * blackWhite.output.Width + j];
                    }
                    else
                    {
                        sum += matrix[(i) * blackWhite.output.Width + j];
                    }

                }
            return sum;

        }
    }
}
